import Vue from "vue";
import { sleep } from "Yan/utils";
// import store from '@/store'
import ContextMenu from "Yan/components/ContextMenu"
/**
* @comoonent 组件
* @e 右键点击回传
* @option 组件传入参数 props
*/
let oldMenu = null
let docBody = null

const ClearOldMenu = () => {
  if (oldMenu) {
    oldMenu.$destroy()
    docBody.removeChild(oldMenu.$el)
    oldMenu = null
    docBody = null
    document.body.removeEventListener('click', ClearOldMenu)
    document.body.removeEventListener('contextMenu', ClearOldMenu)
  }
}
export default async function defaultObj(e, { title, menu, ElementKey }) {
  ClearOldMenu()
  const MenuCreater = Vue.extend(ContextMenu)
  const Menu = new MenuCreater().$mount()
  document.body.appendChild(Menu.$el)
  if (ElementKey) docBody = document.querySelector(ElementKey)
  else docBody = document.body
  docBody.appendChild(Menu.$el)

  Menu.$props.position = { top: e.y, left: e.x };
  Menu.$props.title = title
  Menu.$props.menu = menu
  Menu.$props.afterClick = ClearOldMenu
  await sleep(100)
  oldMenu = Menu
  document.body.addEventListener('click', ClearOldMenu)
  document.body.addEventListener('contextMenu', ClearOldMenu)
}
